{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gp import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<gp.fwrapper at 0x3eb4ed0>,\n",
       " <gp.fwrapper at 0x3eb4f10>,\n",
       " <gp.fwrapper at 0x3eb4f30>,\n",
       " <gp.fwrapper at 0x3eb4f50>,\n",
       " <gp.fwrapper at 0x3eb4ef0>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "exampletree_1=exampletree()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exampletree_1.evaluate([2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "exampletree_1.evaluate([5,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "if\n",
      " isgreater\n",
      "  p0\n",
      "  3\n",
      " add\n",
      "  p1\n",
      "  5\n",
      " subtract\n",
      "  p1\n",
      "  2\n"
     ]
    }
   ],
   "source": [
    "exampletree_1.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "random1=makerandomtree(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random1.evaluate([7,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "49"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random1.evaluate([2,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "random2=makerandomtree(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random2.evaluate([5,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random2.evaluate([5,20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "multiply\n",
      " 7\n",
      " 7\n"
     ]
    }
   ],
   "source": [
    "random1.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "random2.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "hiddenset=buildhiddenset()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "125218"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scorefunction(random2,hiddenset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "115918"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scorefunction(random1,hiddenset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "multiply\n",
      " 7\n",
      " 7\n"
     ]
    }
   ],
   "source": [
    "random1.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "muttree=mutate(random1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "multiply\n",
      " p0\n",
      " 7\n"
     ]
    }
   ],
   "source": [
    "muttree.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "cross=crossover(random1,muttree)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "multiply\n",
      " p0\n",
      " p0\n"
     ]
    }
   ],
   "source": [
    "cross.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "hiddenset=buildhiddenset()\n",
    "rf=getrankfunction(hiddenset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter 0 : 21355.000000\n",
      "iter 1 : 6991.000000\n",
      "iter 2 : 2061.000000\n",
      "iter 3 : 2061.000000\n",
      "iter 4 : 1957.000000\n",
      "iter 5 : 334.000000\n",
      "iter 6 : 334.000000\n",
      "iter 7 : 334.000000\n",
      "iter 8 : 334.000000\n",
      "iter 9 : 334.000000\n",
      "iter 10 : 334.000000\n",
      "iter 11 : 58.000000\n",
      "iter 12 : 58.000000\n",
      "iter 13 : 0.000000\n",
      "subtract\n",
      " subtract\n",
      "  multiply\n",
      "   add\n",
      "    p0\n",
      "    4\n",
      "   p0\n",
      "  subtract\n",
      "   p0\n",
      "   4\n",
      " if\n",
      "  if\n",
      "   multiply\n",
      "    if\n",
      "     isgreater\n",
      "      p1\n",
      "      6\n",
      "     subtract\n",
      "      1\n",
      "      subtract\n",
      "       isgreater\n",
      "        multiply\n",
      "         0\n",
      "         isgreater\n",
      "          8\n",
      "          p0\n",
      "        p1\n",
      "       p0\n",
      "     p1\n",
      "    multiply\n",
      "     0\n",
      "     if\n",
      "      isgreater\n",
      "       4\n",
      "       3\n",
      "      p1\n",
      "      add\n",
      "       7\n",
      "       add\n",
      "        6\n",
      "        isgreater\n",
      "         if\n",
      "          7\n",
      "          multiply\n",
      "           p0\n",
      "           p1\n",
      "          add\n",
      "           p0\n",
      "           p0\n",
      "         p0\n",
      "   5\n",
      "   isgreater\n",
      "    isgreater\n",
      "     subtract\n",
      "      p0\n",
      "      multiply\n",
      "       p1\n",
      "       multiply\n",
      "        7\n",
      "        add\n",
      "         p0\n",
      "         6\n",
      "     5\n",
      "    multiply\n",
      "     p0\n",
      "     if\n",
      "      isgreater\n",
      "       4\n",
      "       4\n",
      "      p1\n",
      "      p0\n",
      "  1\n",
      "  subtract\n",
      "   subtract\n",
      "    3\n",
      "    p1\n",
      "   add\n",
      "    p1\n",
      "    4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<gp.node at 0x4344b70>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evolve(2,500,rf,mutationrate=0.2,breedingrate=0.1,pexp=0.7,pnew=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p1=makerandomtree(5)\n",
    "p2=makerandomtree(5)\n",
    "gridgame([p1,p2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter 0 : 198.000000\n",
      "iter 1 : 198.000000\n",
      "iter 2 : 198.000000\n",
      "iter 3 : 198.000000\n",
      "iter 4 : 198.000000\n",
      "iter 5 : 198.000000\n",
      "iter 6 : 198.000000\n",
      "iter 7 : 198.000000\n",
      "iter 8 : 198.000000\n",
      "iter 9 : 198.000000\n",
      "iter 10 : 198.000000\n",
      "iter 11 : 198.000000\n",
      "iter 12 : 198.000000\n",
      "iter 13 : 198.000000\n",
      "iter 14 : 198.000000\n",
      "iter 15 : 198.000000\n",
      "iter 16 : 198.000000\n",
      "iter 17 : 198.000000\n",
      "iter 18 : 198.000000\n",
      "iter 19 : 198.000000\n",
      "iter 20 : 198.000000\n",
      "iter 21 : 198.000000\n",
      "iter 22 : 198.000000\n",
      "iter 23 : 198.000000\n",
      "iter 24 : 198.000000\n",
      "iter 25 : 198.000000\n",
      "iter 26 : 198.000000\n",
      "iter 27 : 198.000000\n",
      "iter 28 : 198.000000\n",
      "iter 29 : 198.000000\n",
      "iter 30 : 198.000000\n",
      "iter 31 : 198.000000\n",
      "iter 32 : 198.000000\n",
      "iter 33 : 198.000000\n",
      "iter 34 : 198.000000\n",
      "iter 35 : 198.000000\n",
      "iter 36 : 198.000000\n",
      "iter 37 : 198.000000\n",
      "iter 38 : 198.000000\n",
      "iter 39 : 198.000000\n",
      "iter 40 : 198.000000\n",
      "iter 41 : 198.000000\n",
      "iter 42 : 198.000000\n",
      "iter 43 : 198.000000\n",
      "iter 44 : 198.000000\n",
      "iter 45 : 198.000000\n",
      "iter 46 : 198.000000\n",
      "iter 47 : 198.000000\n",
      "iter 48 : 198.000000\n",
      "iter 49 : 198.000000\n",
      "isgreater\n",
      " 9\n",
      " p1\n"
     ]
    }
   ],
   "source": [
    "winner=evolve(5,100,tournament,maxgen=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ". . . . \n",
      ". . . . \n",
      ". X . . \n",
      ". . . O \n",
      "Your last move was -1\n",
      " 0\n",
      "2 3\n",
      " 1\n",
      "Enter move:  3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gridgame([winner,humanplayer()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def makerandomtree(pc,datatype,maxdepth=4,fpr=0.5,ppr=0.5):\n",
    "    if random()<fpr and maxdepth>0:\n",
    "        f=choice(flist[datatype])\n",
    "        children=[makerandomtree(pc,type,maxdepth-1,fpr,ppr) for type in f.params]\n",
    "        return node(f,children)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "\t\n",
    "#flist={'str':[substringw,concatw],'int':[indexw]}\n",
    "flist=[addw,mulw,ifw,gtw,subw]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
